import {formatDate, deleteRightTodo, deleteRightItem, spliceGetDataTodo, getSaleNum, getValueArray, setEmptyArray, setArrayValue, setExchangeValue, spliceGetDataItem} from '@/views/sales/common/TableSaleMethods'
export default ({
  state: {
    Giftsaleform: {
      name: '',
      date: [{
        'value': '',
        index: 1,
        status: 1
      }],
      grade: [],
      least: '1',
      remarks: ''
    },
    saleData: [],
    giftData: [],
    GetsaleData: [],
    GetgiftData: []
  },
  mutations: {
    onAddDate(state, index) { //赠品促销添加日期
      if (state.Giftsaleform.date.length < 3) {
        state.Giftsaleform.date.push({
          'value': '',
          index: index,
          status: 1
        })
      }
    },
    getgiftData (state, type) {
      let getdata = JSON.parse(JSON.stringify(state.saleData));
      // state.GetsaleData = JSON.parse(JSON.stringify(state.saleData));
      getValueArray(getdata, {
          index: type,
          childName: "productSkuList",
          status: false
        });
        setEmptyArray(getdata, {
          index: type,
          childName: "productSkuList"
        });
        setArrayValue(getdata, {
          index: type,
          val1: false,
          childName: "productSkuList"
        });
          if(!state.GetsaleData.length) { //首次为空
              state.GetsaleData = getdata
          } else { // 防止每次更新的淹没其他的
              //把没有的第一层数据存在，GetsaleData里面
              //把没有的第二层的数据存在, GetsaleData里面
              spliceGetDataItem(getdata, state.GetsaleData)
              spliceGetDataTodo(getdata, state.GetsaleData)
          }
        if(type !== 'buttonChecked'){
          setExchangeValue(state.saleData, {index1: 'checked', index: 'buttonChecked',childName: 'productSkuList'})
        }
    },
    getgiftGiftData (state, type) {
      let getdata = JSON.parse(JSON.stringify(state.giftData));
      // state.GetsaleData = JSON.parse(JSON.stringify(state.saleData));
      getValueArray(getdata, {
          index: type,
          childName: "productSkuList",
          status: false
        });
        setEmptyArray(getdata, {
          index: type,
          childName: "productSkuList"
        });
        setArrayValue(getdata, {
          index: type,
          val1: false,
          childName: "productSkuList"
        });
          if(!state.GetgiftData.length) { //首次为空
              state.GetgiftData = getdata
          } else { // 防止每次更新的淹没其他的
              //把没有的第一层数据存在，GetgiftData里面
              //把没有的第二层的数据存在, GetgiftData里面
              spliceGetDataItem(getdata, state.GetgiftData)
              spliceGetDataTodo(getdata, state.GetgiftData)
          }
        if(type !== 'buttonChecked'){
          setExchangeValue(state.giftData, {index1: 'checked', index: 'buttonChecked',childName: 'productSkuList'})
        }   
    },
    deleteGiftData (state, object) {
      if(object.todo && !object.todo.buttonChecked || object.item && !object.item.buttonChecked){
        if(object.todo) { //没子集
            deleteRightTodo(object.item, object.todo, state.GetsaleData)
        }else { //有子集
            deleteRightItem(object.item, state.GetsaleData)
        }
      }  
    },
    deleteGiftGiftData (state, object) {
      if(object.todo && !object.todo.buttonChecked || object.item && !object.item.buttonChecked){
        if(object.todo) { //没子集
            deleteRightTodo(object.item, object.todo, state.GetgiftData)
        }else { //有子集
            deleteRightItem(object.item, state.GetgiftData)
        }
      }  
    },
    onReduceDate(state, index) { //赠品促销减去日期
      state.Giftsaleform.date.splice(index, 1)
    },
    getSaleData(state) {
      state.GetsaleData = JSON.parse(JSON.stringify(state.saleData));
    },
    GetgiftData(state) {
      state.GetgiftData = JSON.parse(JSON.stringify(state.giftData));
    },
    setGiftSaleEmpty(state) {
      state.GetsaleData = ''
      state.saleData = ''
      state.giftData = ''
      state.GetgiftData = ''
      state.Giftsaleform = {
        name: '',
        date: [{
          'value': '',
          index: 1,
          status: 1
        }],
        grade: [],
        least: '1',
        remarks: ''
      }
    }
  },
  actions: {},
  getters: {
    giftTimeList(state) {
      let arrayTime = [];
      if (state.Giftsaleform.date.length == 1) {
        arrayTime = [{
          startDatetime: formatDate(
            state.Giftsaleform.date[0].value[0],
            true
          ),
          endDatetime: formatDate(
            state.Giftsaleform.date[0].value[1],
            true
          )
        }]
      } else if (state.Giftsaleform.date.length == 2) {
        arrayTime = [{
            startDatetime: formatDate(
              state.Giftsaleform.date[0].value[0],
              true
            ),
            endDatetime: formatDate(
              state.Giftsaleform.date[0].value[1],
              true
            )
          },
          {
            startDatetime: formatDate(
              state.Giftsaleform.date[1].value[0],
              true
            ),
            endDatetime: formatDate(
              state.Giftsaleform.date[1].value[1],
              true
            )
          }
        ]
      } else if (state.Giftsaleform.date.length == 3) {
        arrayTime = [{
            startDatetime: formatDate(
              state.Giftsaleform.date[0].value[0],
              true
            ),
            endDatetime: formatDate(
              state.Giftsaleform.date[0].value[1],
              true
            )
          },
          {
            startDatetime: formatDate(
              state.Giftsaleform.date[1].value[0],
              true
            ),
            endDatetime: formatDate(
              state.Giftsaleform.date[1].value[1],
              true
            )
          },
          {
            startDatetime: formatDate(
              state.Giftsaleform.date[2].value[0],
              true
            ),
            endDatetime: formatDate(
              state.Giftsaleform.date[2].value[1],
              true
            )
          }
        ]
      }
      return arrayTime
    },
    giftSaleNum(state) {
      return getSaleNum(state.GetgiftData, {
        childName: 'productSkuList'
      })
    },
    getGiftSaleNum(state) {
      return getSaleNum(state.GetsaleData, {
        childName: 'productSkuList'
      })
    },
    giftSaleGrade(state, getters) { //赠品促销活动对象
      let newarray = []
      for (let item of state.Giftsaleform.grade) {
        for (let todo of getters.storeTypeData) {
          if (item == todo.value) {
            newarray.push(todo.text)
          }
        }
      }
      return newarray.join(',')
    }
  }
})
